﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Api.ContentProvider;
using Api.Model;
using Core.ScreenStore.View;
using CThru.Silverlight;
using NUnit.Framework;
using TypeMock.ArrangeActAssert;

namespace Core.ScreenStore.Test
{
    [TestFixture]
    public class ArticleDetailsViewTest
    {
        [Test, SilverlightUnitTest]
        public void MouseEnter_Content_MouseOverState()
        {
            var ctrl = new ArticleDetailsView();
            SilverUnit.SetTemplateChild(ctrl, "Content", new StackPanel());
            ctrl.OnApplyTemplate();

            ArticleDetailsView c = ctrl;

            var args = SilverUnit.MakeEventArgs<MouseEventArgs>();
            SilverUnit.FireEvent(c.Content, UIEvents.MouseEnter, c.Content, args);

            SilverUnit.Assert.VisualStatedWasChangedTo("MouseOver", ctrl, true);
        }

        [Test, SilverlightUnitTest]
        public void SetPropertyArticle_Article_CallingFillArticleList()
        {
            var ctrl = new ArticleDetailsView();
            ctrl.Article = Isolate.Fake.Instance<Article>();
            Isolate.Verify.WasCalledWithAnyArguments(() => ctrl.FillArticleList());
        }

        [Test, SilverlightUnitTest]
        public void SetPropertyContentProvider_ContentProvider_CallingFillArticleList()
        {
            var ctrl = new ArticleDetailsView();
            ctrl.ContentProvider = Isolate.Fake.Instance<IContentProvider>();
            Isolate.Verify.WasCalledWithAnyArguments(() => ctrl.FillArticleList());
        }

        [Test, SilverlightUnitTest]
        public void Constructor_ContentArticle_IsNull()
        {
            var ctrl = new ArticleDetailsView();
            Assert.IsNull(ctrl.Content);
            Assert.IsNull(ctrl.Article);
        }

        [Test, SilverlightUnitTest]
        public void FillArticleList_Content_IsNull()
        {
            UIElement fakeUIElement = Isolate.Fake.Instance<UIElement>();
            var ctrl = new ArticleDetailsView();
            ctrl.FillArticleList();
            Assert.IsNull(ctrl.Content);
        }

        [Test, SilverlightUnitTest]
        public void OnApplyTemplate_Content_IsNotNull()
        {
            var ctrl = new ArticleDetailsView();
            SilverUnit.SetTemplateChild(ctrl, "Content", new StackPanel());

            ctrl.OnApplyTemplate();

            Assert.IsNotNull(ctrl.Content);
            Assert.IsNull(ctrl.Article);
        }

        [Test, SilverlightUnitTest]
        public void OnApplyTemplate_Content_ChildrenIsNotNull()
        {
            var ctrl = new ArticleDetailsView();
            SilverUnit.SetTemplateChild(ctrl, "Content", new StackPanel());
            ctrl.Article = Isolate.Fake.Instance<Article>();

            ctrl.OnApplyTemplate();

            Assert.IsNotNull(ctrl.Content);
            Assert.IsNotNull(ctrl.Content.Children);
        }

        [Test, SilverlightUnitTest]
        public void OnApplyTemplate_Content_CalledFillArticleList()
        {
            var ctrl = new ArticleDetailsView();
            ctrl.OnApplyTemplate();
            Isolate.Verify.WasCalledWithAnyArguments(() => ctrl.FillArticleList());
        }

        [Test, SilverlightUnitTest]
        public void OnApplyTemplate_Content_AddCalled()
        {
            var ctrl = new ArticleDetailsView();
            var content = new StackPanel();
            var contentChild = content.Children;
            Isolate.WhenCalled(() => contentChild.Add(null)).IgnoreCall();

            SilverUnit.SetTemplateChild(ctrl, "Content", content);
            ctrl.Article = Isolate.Fake.Instance<Article>();
            Isolate.WhenCalled(() => ctrl.Article.Items).DoInstead(context => new List<Item>() { new Item() { Type = "text", Value = "blabla" } });

            var contentProvider = Isolate.Fake.Instance<IContentProvider>();
            Isolate.WhenCalled(() => contentProvider.GetItemsMapping(null))
                                                    .DoInstead(context => (type => new TextBlock()));
            ctrl.ContentProvider = contentProvider;

            ctrl.OnApplyTemplate();

            Isolate.Verify.WasCalledWithAnyArguments(() => contentChild.Add(null));         
        }
    }
}
